<?php


// include_once('init.php');
// print_r($_POST);
if (!empty($_POST)) {
    //实现数据库链接mysqli_connect的函数，数据不为空时要执行的操作
    //从$_POST获取到的用户数据存到临时变量中
    $username = $_POST['username'];
    $password=md5($_POST['password']);

    $Code = $_POST['code'];

    $code=strtolower($Code);
    
    // echo $_SESSION['code'];die;

    if($code!=strtolower($_SESSION['code'])){

        echo '<script>alert("验证码错误");history.go(-1);</script>';
        die;

    }

    // echo $code;die;
   
 
if (!empty($_COOKIE['username']) && $_COOKIE['password'] == $_POST['password']) {
    $password = $_COOKIE['password'];
   } 

  
    // $where['username']=$username ;
    // $where['password']=$password ;
     
   

    $data = getSelectByOne('admin,role',"`admin`.role_id=`role`.id and `username` = '$username' and `password`='$password'");
   
   // print_r($data);die;
    //如果数据不为空时
    if (!empty($data)) {
        // session_start();

        $data['power'] = explode(',', $data['power']);
// print_r($data);die;



        $_SESSION['admin'] = $data;

// print_r($data);die;


        if (!empty($_POST['check'])) {
            // echo 1;exit;
            setcookie('username', $username, time() + 3600 * 24);
            setcookie('password', $password, time() + 3600 * 24);
        } else {
            setcookie('username', '', 0);
            setcookie('password', '', 0);
        }

        if(!empty($_POST['auto'])){

            setcookie('auto_login',1, time() + 3600 * 24);

        }else{

            setcookie('auto_login', '0', 0);
        }

unset($_SESSION['code']);

        // echo '<script>alert("登陆成功");</script>';die;
        header('location:index.php?&c=index&a=index&admin=1');
        //显示登陆成功提示
    } else {
        echo '<script>alert("用户名或密码错误");history.go(-1);</script>';
        die;
        //否则就显示错误
    }
}



include(VIEW_PATH.$admin.'admin/login.html');

?>

